from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis import get_redis_connection

from utils.captcha.captcha import captcha


class ImageCodeView(View):
    def get(self, request, uuid):
        # 步骤
        # 1 获取uuid
        # print(uuid)
        # 2 生成图片验证码 和二进制图片数据
        text, image = captcha.generate_captcha()
        print('图片验证码：',text)
        # 3 redis保存验证码 用uuid为key 有效期
        redis_cli = get_redis_connection("code")
        # uuid为key  120s是过期时间
        redis_cli.setex(uuid, 120, text)
        # 4 返回二进制图片数据

        return HttpResponse(image, content_type='image/jpeg')
